package com.xcf.mybatis_example.controller;

import com.xcf.mybatis_example.dao.mapper.UserMapper;
import com.xcf.mybatis_example.dao.pojo.User;
import com.xcf.mybatis_example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("poolTaskExecutor")
    private ThreadPoolTaskExecutor poolTaskExecutor;

    @GetMapping("/session")
    public User test1() {
        System.out.println("=========session");
        User user = new User( );
        user.setId(1);
        user.setUsername("zhansan");
        System.out.println("=========1");
        userMapper.findOne(user);
        System.out.println(" =======2========= ");
        userMapper.findByUsername(user);
        System.out.println("========3==========");
        return userMapper.findOne(user);
    }

    @GetMapping("/session2")
    public User test2() {
        System.out.println("=========session2");
        User user = new User( );
        user.setId(1);
        return userMapper.findOne(user);
    }

    @GetMapping("/test")
    public String test3() {
        User user = new User( );
        user.setUsername("xcf")
                .setNum(1L);
//        userService.update(user);
        for (int i = 0; i < 10; i++) {
            poolTaskExecutor.execute(() -> {
                userService.updateData(user);
                userService.updateData(user);
            });
        }

        return "SUCCESS";
    }
}
